<template>
	<view class="container" v-if="Object.keys(goods).length !== 0">
		<!-- 轮播图 -->
		<swiper circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in goods.pics" :key="index">
				<view class="swiper-item">
					<image :src="item.pics_big" @click="preview(index)"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 文字信息 -->
		<view class="info">
			<!-- 价格 -->
			<view class="price">
				¥ {{goods.goods_price}}
			</view>
			<!-- 名称和收藏 -->
			<view class="mid">
				<view class="title">
					{{goods.goods_name}}
				</view>
				<view class="favi">
					<uni-icons color="gray" type="star" size="20"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">
				快递：免运费
			</view>
		</view>
		<!-- 商品详情 -->
		<!-- <rich-text></rich-text> -->
		<rich-text :nodes="goods.goods_introduce"></rich-text>
		<!-- 商品导航 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {mapMutations,mapGetters} from 'vuex'
	export default {
		data() {
			return {
				goods: {},
				options: [{
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		computed:{
			...mapGetters('m_cart',['allGoodsCount'])
		},
		onLoad(options) {
			this.getGoods(options.goods_id)
		},
		methods: {
			...mapMutations('m_cart',['updateAddCart']),
			// 左侧图片点击事件
			onClick(e) {
				// console.log(e)
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			// 按钮事件
			buttonClick(e) {
				// console.log(e)
				if (e.content.text === '加入购物车') {
					// 准备精简版的数据
					// id 名称 价格 图片 数量 勾选状态
					const goods = {
						goods_id: this.goods.goods_id,
						goods_name: this.goods.goods_name,
						goods_price: this.goods.goods_price,
						goods_small_logo:this.goods.goods_small_logo,
						goods_count:1,
						goods_state:true
					}
					this.updateAddCart(goods)
				}
			},
			// 预览轮播图
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goods.pics.map((e) => {
						return e.pics_big
					})
				})
			},
			// 获取详情页面
			async getGoods(goods_id) {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				// console.log(res)
				if (meta.status !== 200) return uni.$showMsg()
				message.goods_introduce = message.goods_introduce.replace(/<img/g, '<img style="display:block"')
				this.goods = message
			}
		},
		watch:{
			allGoodsCount:{
				handler(newCount){
					this.options[1].info = newCount
				},
				immediate:true
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.info {
		padding: 6px;

		.price {
			font-size: 20px;
			color: #ea4350;
		}

		.mid {
			display: flex;
			margin: 6px 0;
			font-size: 12px;

			.favi {
				border-left: 1px solid #eee;
				width: 120px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: gray;
			}
		}

		.yf {
			font-size: 10px;
			color: gray;
		}

	}

	.goods-nav {
		position: fixed;
		left: 0;
		bottom: 0;
		right: 0;
	}

	.container {
		padding-bottom: 50px;
	}
</style>
